using System;
using FireStarterModeller.BO;

namespace FireStarterModeller.Logic
{
    /// <summary>
    /// Deletes the Relationship Properties and it owner Property or Related Property as long as neither of these form part of the
    /// Primary Key <see cref="DMObjectIdentity"/> of the <see cref="DMClass"/>.
    /// </summary>
    public class DMRelationshipPropertyDeleter
    {
        private readonly IDMRelationshipProperty _relationshipProperty;

        public DMRelationshipPropertyDeleter(IDMRelationshipProperty relationshipProperty)
        {
            _relationshipProperty = relationshipProperty;
            if (_relationshipProperty == null) throw new ArgumentNullException("relationshipProperty");
        }

        /// <summary>
        /// Deletes the <see cref="DMRelationshipProperty"/>. For Details See <see cref="DMRelationshipPropertyDeleter"/>
        /// </summary>
        public void Delete()
        {
            IDMProperty ownerProperty = _relationshipProperty.OwnerProperty;
            IDMProperty relatedProperty = _relationshipProperty.RelatedProperty;
            _relationshipProperty.MarkForDelete();
            if (ownerProperty.IsNotNull() && !ownerProperty.IsPartOfObjectIdentity)
            {
                ownerProperty.MarkForDelete();
            }
            else if (relatedProperty.IsNotNull() && !relatedProperty.IsPartOfObjectIdentity)
            {
                relatedProperty.MarkForDelete();
            }
        }
    }
}